package com.wjbgn.bsolver.server.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.wjbgn.bsolver.server.api.UserClient;
import com.wjbgn.bsolver.server.dto.BugAnsweredRecordDTO;
import com.wjbgn.bsolver.server.dto.UserDTO;
import com.wjbgn.bsolver.server.service.IBugAnswerAcceptRejectService;
import com.wjbgn.bsolver.server.util.UserInfoCacheUtil;
import com.wjbgn.stater.dto.Result;
import com.wjbgn.stater.enums.CommonReturnEnum;
import com.wjbgn.stater.util.redis.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: bug回复的用户采纳和无用记录
 * Create Date: 2022-05-11T14:01:17.965
 *
 * @author wjbgn
 * @version 1.0
 */
@Service
public class BugAnswerAcceptRejectServiceImpl implements IBugAnswerAcceptRejectService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private UserInfoCacheUtil userInfoCacheUtil;

    @Override
    public boolean updateAcceptReject(BugAnsweredRecordDTO bugAnsweredRecordDTO) {

        Long userId = userInfoCacheUtil.getUserIdByUsername(bugAnsweredRecordDTO.getUsername());
        // 采纳
        boolean acceptBoolean = redisUtil.getBitmap("answered:accept:" + bugAnsweredRecordDTO.getId(), userId);
        // 无用
        boolean rejectBoolean = redisUtil.getBitmap("answered:reject:" + bugAnsweredRecordDTO.getId(), userId);
        // 无用
        if ("reject".equals(bugAnsweredRecordDTO.getFlag())) {
            //设置拒绝
            if (rejectBoolean) {
                redisUtil.setBitmap("answered:reject:" + bugAnsweredRecordDTO.getId(), userId, false);
            } else {
                if (acceptBoolean) {
                    redisUtil.setBitmap("answered:accept:" + bugAnsweredRecordDTO.getId(), userId, false);
                }
                redisUtil.setBitmap("answered:reject:" + bugAnsweredRecordDTO.getId(), userId, true);
            }
        } else {
            // 采纳
            if (acceptBoolean) {
                redisUtil.setBitmap("answered:accept:" + bugAnsweredRecordDTO.getId(), userId, false);
            } else {
                if (rejectBoolean) {
                    redisUtil.setBitmap("answered:reject:" + bugAnsweredRecordDTO.getId(), userId, false);
                }
                redisUtil.setBitmap("answered:accept:" + bugAnsweredRecordDTO.getId(), userId, true);
            }
        }
        // redis记录当日被更新的回答的id
        redisUtil.setSetValue("answered:num:" + DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd"), bugAnsweredRecordDTO.getId());
        // 记录每日有操作的用户
        boolean isAccept = redisUtil.getBitmap("answered:accept:" + bugAnsweredRecordDTO.getId(), userId);
        boolean isReject = redisUtil.getBitmap("answered:reject:" + bugAnsweredRecordDTO.getId(), userId);
        bugAnsweredRecordDTO.setCurrentHasAccept(isAccept);
        bugAnsweredRecordDTO.setCurrentHasReject(isReject);
        redisUtil.leftPush("answered:user:" + DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd"), bugAnsweredRecordDTO);
        return true;
    }

    /**
     * 根据answerId获取
     *
     * @param answerId
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     * @author weirx
     * @date: 2022/5/16
     */
    public List<JSONObject> listByAnswerId(String answerId) {
        // 获取当前用户针对当前回复的采纳或点赞记录
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("bsolver_bug_answer_accept_reject");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        //各种查询
        queryBuilder.must(QueryBuilders.termQuery("answerId", answerId));
        sourceBuilder.query(queryBuilder);
        searchRequest.source(sourceBuilder);
        SearchResponse rp = null;
        try {
            rp = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<JSONObject> list = new ArrayList<>();
        for (SearchHit searchHit : rp.getHits()) {
            String sourceAsString = searchHit.getSourceAsString();
            JSONObject jsonObject = JSONObject.parseObject(sourceAsString);
            list.add(jsonObject);
        }
        return list;
    }
}